package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyReply;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyReplyMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyReplyQueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategyReplyMapper strategyReplyMapper;

    @Override
    public void saveOrUpdate(User u) {
        if(u.getId() == null){
            u.setPlace("中国");
            u.setHeadImgUrl("/img/user/head.jpg");
            u.setCoverImgUrl("/img/user/bg.jpeg");
            u.setGender(User.GENDER_NONE);
            u.setSign("来玩啊");
            userMapper.insert(u);
        }else {
            userMapper.updateByPrimaryKey(u);
        }
    }

    @Override
    public void regist(User u) {
        User user = userMapper.checkEmail(u.getEmail());
        if(user != null){
            throw new RuntimeException("该账号已经存在");
        }
        saveOrUpdate(u);
    }

    @Override
    public User login(String email, String password) {
        User u =  userMapper.selectByInfo(email,password);
        if(u == null){
            throw new RuntimeException("账号或密码错误");
        }
        return u;
    }

    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<User>(list);
    }

    @Override
    public User get(Long id) {
        User u = userMapper.selectByPrimaryKey(id);
        u.setCollectCount(collectMapper.selectForCount(id));
        u.setCommentCount(strategyCommentMapper.selectForCount(id));
        return u;
    }

    @Override
    public PageInfo<User> queryFollowAndFan(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<User> list = new ArrayList<>();
        if("fans".equals(qo.getType())){
                list=userMapper.selectFans(qo.getUserId());
        }else {
            list=userMapper.selectFollows(qo.getUserId());
        }
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo queryReceiveComments(StrategyReplyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
       List<StrategyReply> list = strategyReplyMapper.selectForReceiveComments(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Integer> countForAllNoRead(Long userId) {
        return userMapper.countForAllNoRead(userId);
    }

    public List<User> list() {
        return userMapper.selectAll();
    }


    /*public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }*/
}
